﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base;
using UFIDA.U9.Base.FlexField.KeyFlexField;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.CBO.PubSV.Helper;
using UFIDA.U9.CBO.SCM.Customer;
using UFIDA.U9.CBO.SCM.Item;
using UFSoft.UBF.Business;
using UFSoft.UBF.PL;
using UFSoft.UBF.Util.Context;
using CustomerDTO = UFIDA.U9.ISV.Customer.CustomerDTO;
using CustomerDTOData = UFIDA.U9.ISV.Customer.CustomerDTOData;
using CustomerSiteDTO = UFIDA.U9.ISV.Customer.CustomerSiteDTO;

namespace ufida.u9.ait.openapi.utils
{
    internal class CustomerUtils
    {
		/// <summary>
		/// 客户新增
		/// 该代码参考 BatchCreateCustomerByDTOSV 修改
		/// </summary>
		/// <param name="CustomerDTOs"></param>
		/// <returns></returns>
		public static List<Customer> AITBatchCreateCustomerByDTOSV(List<CustomerDTOData> CustomerDTOs)
		{
			List<Customer> entityList = new List<Customer>();
			foreach (CustomerDTOData customerDTOData in CustomerDTOs)
			{
				long orgid = Context.LoginOrg.ID;
				string orgcode=Context.LoginOrg.Code;
				//组织=登录组织
                customerDTOData.Org = new CommonArchiveDataDTOData
                {
                    ID = orgid,
                    Code = orgcode,
                };

                //将DTOData转为DTO对象
                CustomerDTO customerDTO = new CustomerDTO();
				customerDTO.FromEntityData(customerDTOData);

				using (ISession session = Session.Open())
				{
					Customer customer = Customer.Create();

					customer.KeyFlexFieldStru = KeyFlexFieldStru.Finder.FindByID(2);
					//将DTO转换为BE
					CBOCommonHelper.DtoToEntity(customerDTO, customer, delegate (DTOToEntityEventArgs<CustomerDTO, UFIDA.U9.CBO.SCM.Customer.Customer> e)
					{
						e.OrgID = orgid;
						if (e.AttributeName == "CustomerSiteDTOs" || e.AttributeName == "DTOForCRM")
						{
							e.Cancel = true;
						}
					});
					
					if (customerDTO.CustomerSiteDTOs != null && customerDTO.CustomerSiteDTOs.Count > 0)
					{
                        //客户位置
                        foreach (CustomerSiteDTO customerSiteDTO in customerDTO.CustomerSiteDTOs)
						{
							CustomerSite be = CustomerSite.Create(customer);
							CBOCommonHelper.DtoToEntity(customerSiteDTO, be, delegate (DTOToEntityEventArgs<CustomerSiteDTO, CustomerSite> e)
							{
								e.OrgID = orgid;
								if (e.AttributeName == "OperateType" || e.AttributeName == "ID")
								{
									e.Cancel = true;
								}
							});
						}
					}
					session.Commit();
					entityList.Add(customer);
				}
			}
			return entityList;
		}

        

        /// <summary>
        /// 客户修改
        /// 该代码参考 BatchModifyCustomerByDTOSV 修改
		/// 只要是DTO上存在的字段,均会赋值到BE上
        /// </summary>
        /// <param name="CustomerDTOs"></param>
        /// <returns></returns>
        /// <exception cref="CommonCodeSVException"></exception>
        public static List<Customer> AITBatchModifyCustomerByDTOSV(List<CustomerDTOData> CustomerDTOs)
        {
			List<Customer> retlist = new List<Customer>();
			foreach (CustomerDTOData customerDTOData in CustomerDTOs)
			{
                long orgid = Context.LoginOrg.ID;
                string orgcode = Context.LoginOrg.Code;
                //组织=登录组织
                customerDTOData.Org = new CommonArchiveDataDTOData
                {
                    ID = orgid,
                    Code = orgcode,
                };

                //将DTOData转为DTO对象
                CustomerDTO customerDTO = new CustomerDTO();
				customerDTO.FromEntityData(customerDTOData);

				//启用session,对客户/客户位置BE进行新增或修改
				using (ISession session = Session.Open())
				{
					//查找客户
					Customer customer = Customer.Finder.Find("Code=@Code and Org.Code=@OrgCode", new OqlParam[]
								{
										new OqlParam(customerDTO.Code),
										new OqlParam(orgcode)
								});
					if (customer == null)
						throw new Exception("客户修改方法,查找客户失败!");

					//将客户/客户位置DTO转为BE,并保存
					if (customer != null)
					{
						//客户处理
						//将DTO属性赋值给BE
						CBOCommonHelper.DtoToEntity<CustomerDTO, Customer>(customerDTO, customer, delegate (DTOToEntityEventArgs<CustomerDTO, Customer> e)
						{
							//代理方法,判断是否需要取消属性赋值
							e.OrgID = orgid;
							if (e.AttributeName == "CustomerSiteDTOs" || e.AttributeName == "DTOForCRM")
							{
								e.Cancel = true;
							}
						});

						if (customerDTO.CustomerSiteDTOs != null && customerDTO.CustomerSiteDTOs.Count > 0)
						{
							//客户位置处理
							foreach (CustomerSiteDTO customerSiteDTO in customerDTO.CustomerSiteDTOs)
							{
								//客户位置查找,条件 位置编码/客户编码/组织编码
								CustomerSite customerSite = CustomerSite.Finder.Find($"Code='{customerSiteDTO.Code}' and Customer.Code='{customerDTO.Code}' and Customer.Org.Code='{customerDTO.Org.Code}'");
								if (customerSite == null)
								{
									//查找不到则创建
									customerSite = CustomerSite.Create(customer);
								}
								//将DTO属性赋值给BE
								CBOCommonHelper.DtoToEntity<CustomerSiteDTO, CustomerSite>(customerSiteDTO, customerSite, delegate (DTOToEntityEventArgs<CustomerSiteDTO, CustomerSite> e)
								{
                                    //代理方法,判断是否需要取消属性赋值
                                    e.OrgID = orgid;
									if (e.AttributeName == "OperateType")
									{
										e.Cancel = true;
									}
								});
							}
						}
						session.Commit();
						retlist.Add(customer);
					}
				}
			}
			return retlist;
		}
    }
}
